package com.karaoke.android.home.vm

//import com.apkfuns.logutils.LogUtils
import android.content.Context
import android.content.Intent
import android.databinding.ObservableField
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import com.github.javiersantos.materialstyleddialogs.MaterialStyledDialog
import com.github.javiersantos.materialstyleddialogs.enums.Style
import com.karaoke.android.data.domain.PlayList
import com.karaoke.android.data.domain.Room
import com.karaoke.android.data.krealmextensions.queryFirst
import com.karaoke.android.data.service.MediaListService
import com.karaoke.android.data.utils.hideSystemBar
import com.karaoke.android.home.R
import com.karaoke.android.library.mvvm.ActivityViewModel
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber

/**
 * Created by Administrator on 2017/4/1.
 */
class MainViewModel : ActivityViewModel()
{
    val room: Room? = Room().queryFirst()
    val selectCount = ObservableField<String>()


    @Subscriber
    fun onMediaListChanged(list: PlayList)
    {
        selectCount.set("${list.list.size}")
    }

    override fun onCreate()
    {
        super.onCreate()
        EventBus.getDefault().register(this)
        selectCount.set("${MediaListService.mediaList.list.size}")
    }

    override fun onDestroy()
    {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    fun showPasswordDialog(view: View): Boolean
    {
        val inflater = view.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val customView = inflater.inflate(R.layout.setting_input_pwd, null)

        val edit = customView.findViewById(R.id.editText) as EditText
        val dialog = MaterialStyledDialog.Builder(view.context)
                .setTitle("系统设置")
                .setDescription("进入系统设置，请输入系统密码")
                .setCustomView(customView)
                .setStyle(Style.HEADER_WITH_TITLE)
                .setPositiveText("确定")
                .onPositive { materialDialog, dialogAction ->
                    val pwd = edit.text.toString()
                    if (pwd == "0909")
                    {
                        view.context.hideSystemBar(false)
                        val intent = Intent(Settings.ACTION_SETTINGS)
                        view.context.startActivity(intent)
                    }
                }
                .show()

        return true
    }
}